   // src/utils/request.js
   import axios from 'axios'
   import { ElMessage, ElMessageBox } from 'element-plus'

   const service = axios.create({
     baseURL: "http://localhost:80/api",
     timeout: 10000
   })

   // 请求拦截器添加token
   service.interceptors.request.use(
     config => {
       const token = localStorage.getItem('token')
       if (token) {
         config.headers['Authorization'] = `Bearer ${token}`
       }
       return config
     },
     error => {
       console.error('请求错误', error)
       return Promise.reject(error)
     }
   )

   // 响应拦截器处理错误
   service.interceptors.response.use(
     response => {
       const res = response.data
       // 如果返回的状态码不是200，则判断为错误
       if (res.code !== 200) {
         ElMessage({
           message: res.message || '请求错误',
           type: 'error',
           duration: 5 * 1000
         })

         // 401: 未登录或token过期
         if (res.code === 401) {
           ElMessageBox.confirm(
             '登录状态已过期，请重新登录',
             '确认退出',
             {
               confirmButtonText: '重新登录',
               cancelButtonText: '取消',
               type: 'warning'
             }
           ).then(() => {
             localStorage.removeItem('token')
             window.location.href = '/login'
           })
         }
         return Promise.reject(new Error(res.message || '请求错误'))
       } else {
         return res
       }
     },
     error => {
       console.error('响应错误', error)
       ElMessage({
         message: error.message || '网络异常，请稍后重试',
         type: 'error',
         duration: 5 * 1000
       })
       return Promise.reject(error)
     }
   )

   export default service